package com.cloudweb.oa.controller;

import cn.js.fan.cache.jcs.RMCache;
import cn.js.fan.db.Conn;
import cn.js.fan.db.ResultIterator;
import cn.js.fan.db.ResultRecord;
import cn.js.fan.util.IpUtil;
import cn.js.fan.util.ParamUtil;
import cn.js.fan.util.StrUtil;
import cn.js.fan.util.XMLConfig;
import cn.js.fan.web.Global;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudweb.oa.entity.Department;
import com.cloudweb.oa.exception.ValidateException;
import com.cloudweb.oa.service.IDepartmentService;
import com.cloudweb.oa.utils.*;
import com.cloudweb.oa.vo.Result;
import com.cloudwebsoft.framework.db.JdbcTemplate;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.oa.Config;
import com.redmoon.oa.dept.DeptDb;
import com.redmoon.oa.flow.FormDb;
import com.redmoon.oa.flow.FormForm;
import com.redmoon.oa.pvg.Privilege;
import com.redmoon.oa.system.OaSysVerMgr;
import com.redmoon.oa.util.TwoDimensionCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.jcs3.access.exception.CacheException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Iterator;

@Api(tags = "应用配置")
@Controller
public class ApplicationController {
    @Autowired
    HttpServletRequest request;

    @Autowired
    ResponseUtil responseUtil;

    @Autowired
    IDepartmentService departmentService;

    @Autowired
    ConfigUtil configUtil;

    @Autowired
    SysProperties sysProperties;

    @ApiOperation(value = "获取配置信息", notes = "获取配置信息", httpMethod = "POST")
    @RequestMapping(value = "/config/getConfigInfo", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;"})
    @ResponseBody
    public Result<Object> getConfigInfo() {
        JSONObject json = new JSONObject();
        Config cfg = Config.getInstance();
        com.redmoon.weixin.Config weixinCfg = com.redmoon.weixin.Config.getInstance();
        boolean isUseWx = weixinCfg.getBooleanProperty("isUse");
        json.put("isPostUsed", cfg.getBoolean("isPostUsed"));

        boolean isSyncWxToOa = weixinCfg.getBooleanProperty("isSyncWxToOA");
        com.redmoon.dingding.Config dingdingCfg = com.redmoon.dingding.Config.getInstance();
        boolean isUseDingDing = dingdingCfg.getBooleanProperty("isUse");
        boolean isSyncDingDingToOa = dingdingCfg.getBooleanProperty("isSyncDingDingToOA");

        json.put("isUseWx", isUseWx);
        json.put("isSyncWxToOA", isSyncWxToOa);
        json.put("isUseDingDing", isUseDingDing);
        json.put("isSyncDingDingToOA", isSyncDingDingToOa);

        return new Result<>(json);
    }

    @ApiOperation(value = "取得界面配置", notes = "取得界面配置", httpMethod = "POST")
    @RequestMapping(value = "/setup/getUiSetup", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;"})
    @ResponseBody
    public Result<Object> getUiSetup() {
        String applicationCode = ParamUtil.get(request, "applicationCode");
        com.redmoon.oa.Config cfg = com.redmoon.oa.Config.getInstance();
        // 前端指定的applicationCode优先
        if (StrUtil.isEmpty(applicationCode)) {
            applicationCode = cfg.get("applicationCode");
        }

        String uiSetup = "{}";
        OaSysVerMgr oaSysVerMgr = new OaSysVerMgr();
        String uiSetups = oaSysVerMgr.getOaSysVer().getUiSetup();
        JSONArray ary = JSONArray.parseArray(uiSetups);
        if (ary == null) {
            ary = new JSONArray();
        }

        boolean isMenuGroupByApplication = cfg.getBooleanProperty("isMenuGroupByApplication");
        if (isMenuGroupByApplication) {
            // 从ary中取出原来对应的设置索引
            for (Object o : ary) {
                JSONObject jsonObject = (JSONObject) o;
                if (jsonObject.containsKey("applicationCode")) {
                    String appCode = jsonObject.getString("applicationCode");
                    if (appCode.equals(applicationCode)) {
                        uiSetup = jsonObject.toString();
                        break;
                    }
                } else {
                    if (StrUtil.isEmpty(applicationCode)) {
                        uiSetup = jsonObject.toString();
                        break;
                    }
                }
            }
        } else {
            if (ary.size() > 0) {
                uiSetup = ary.get(0).toString();
            }
        }
        return new Result<>(uiSetup);
    }

    @ApiOperation(value = "更新界面配置", notes = "更新界面配置", httpMethod = "POST")
    @ApiImplicitParam(name = "uiSetup", value = "模块编码", required = true, dataType = "String")
    @RequestMapping(value = "/setup/updateUiSetup", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;"})
    @ResponseBody
    public Result<Object> updateUiSetup(@RequestParam String uiSetup) {
        String applicationCode = ParamUtil.get(request, "applicationCode");
        com.redmoon.oa.Config cfg = com.redmoon.oa.Config.getInstance();
        // 前端指定的applicationCode优先
        if (StrUtil.isEmpty(applicationCode)) {
            applicationCode = cfg.get("applicationCode");
        }
        JSONObject json = JSONObject.parseObject(uiSetup);
        json.put("applicationCode", applicationCode);

        boolean isMenuGroupByApplication = cfg.getBooleanProperty("isMenuGroupByApplication");
        OaSysVerMgr oaSysVerMgr = new OaSysVerMgr();
        String uiSetupDb = oaSysVerMgr.getOaSysVer().getUiSetup();
        JSONArray ary = JSONArray.parseArray(uiSetupDb);
        if (ary == null) {
            ary = new JSONArray();
        }
        if (isMenuGroupByApplication) {
            // 从ary中取出原来对应的设置索引
            int k = 0;
            boolean isFound = false;
            for (Object o : ary) {
                JSONObject jsonObject = (JSONObject) o;
                if (jsonObject.containsKey("applicationCode")) {
                    String appCode = jsonObject.getString("applicationCode");
                    if (appCode.equals(applicationCode)) {
                        isFound = true;
                        break;
                    }
                } else {
                    // 如果json中没有applicationCode，且applicationCode为空，则说明为默认的uiSetup
                    if ("".equals(applicationCode)) {
                        isFound = true;
                        break;
                    }
                }
                k++;
            }
            // 删除原来的设置
            if (isFound) {
                ary.remove(k);
            }
            ary.add(json);
        } else {
            ary.clear();
            ary.add(json);
        }
        return new Result<>(oaSysVerMgr.updateUiSetup(ary.toString()));
    }
}
